package com.kcsj.service.user;

import com.kcsj.dao.User.RoleDao;
import com.kcsj.domain.user.Role;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
//201902104020 李鑫元
public class RoleService {
    private static final RoleDao roleDao
            = RoleDao.getInstance();
    private static final RoleService roleService
            =new RoleService();
    private RoleService(){}

    public static RoleService getInstance(){
        return roleService;
    }
    public Role find(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Role userRole = roleDao.find(id, conn);
        JdbcHelper.close(conn);
        return userRole;
    }

    public Collection<Role> findAll() throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<Role> roles = roleDao.findAll(conn);
        JdbcHelper.close(conn);
        return roles;
    }
    
    public int add(Role role) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        int id = roleDao.add(role, conn);
        JdbcHelper.close(conn);
        return id;
    }

    public boolean update(Role role) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        boolean update = roleDao.update(role, conn);
        JdbcHelper.close(conn);
        return update;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        boolean delete = roleDao.delete(id, conn);
        JdbcHelper.close(conn);
        return delete;
    }
}
